package cn.studyjava.verify;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;

import cn.studyjava.verify.util.Md5Util;

/**
 * <p>
 * Description: 验证码确认demo
 * </p>
 * 
 * @author: zsljava
 * @version 1.0.0
 * <p>
 * History:
 * -----------------------------------------------
 * @Date: 2017年9月1日 下午3:52:49
 * @author: zsljava
 * @version 1.0.0
 * @OP: Create
 * -----------------------------------------------
 * </p>
 *
 * @since
 * @see
 */
public class AuthSureUtil {

	/**
	 * 
	 * @Title: main
	 * @Description: signature 生成规则 MD5(authId_appKey_timestamp)
	 * 						authId 由验证码提供者提供  appKey 由验证码提供者提供  timestamp当前时间戳
	 * 				返回参数请看readme.md
	 * @param args
	 * @throws IOException
	 * @return: void
	 */
	public static void main(String[] args) throws IOException {
		String appId= "a3a5c1faff9e41c2b2447a52c5bd7ea0";
		String appKey= "a109981dd38540d5b20b4af760d7f6f1";
		String authId = "e97433bb36d34c9db3931b04900e9864";
		String timestamp = String.valueOf(System.currentTimeMillis()/1000);
		String signature = Md5Util.md532(String.format("%s_%s_%s", appId, appKey, timestamp));
//		String key = Md5Utils.md532(appId+"_a109981dd38540d5b20b4af760d7f6f1_"+times);
//		URL u = new URL("http://localhost:28088/v1/auth/e97433bb36d34c9db3931b04900e9864?appId="+appid+"&timestamp="+times+"&signature="+key);
		String url = "http://localhost:28088/v1/auth/%s?appId=%s&timestamp=%s&signature=%s";
		URL u = new URL(String.format(url, authId, appId, timestamp, signature));
		InputStream inputStream = u.openConnection().getInputStream();
		 InputStreamReader inputStreamReader = new InputStreamReader(inputStream);  
	        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);  
	        StringBuilder result = new StringBuilder();  
	        String line = null;  
	        try {  
	            while((line = bufferedReader.readLine()) != null){  
	                result.append(line + "\n");  
	            }  
	        } catch (IOException e) {  
	            e.printStackTrace();  
	        } finally {  
	            try{  
	                inputStreamReader.close();  
	                inputStream.close();  
	                bufferedReader.close();  
	            }catch(IOException e){  
	                e.printStackTrace();  
	            }  
	        }  
	        System.out.println(result.toString());
	}
}
